﻿var express = require('express');
var app = express();
var path = require('path');
var router_red = express.Router();
var router_blue = express.Router();
var ejs = require('ejs');
var session = require('express-session');
var cookieParser = require('cookie-parser');

app.use(express.static('./'));

app.use(cookieParser("12345"));
app.use(session({
	secret: '12345',
	cookie: {maxAge: 800000 },
	resave: false,
	saveUninitialized: true
}));

app.set("views", '/view');

app.set('views', path.join(__dirname, 'views'));
app.engine('html',ejs.__express);
app.set('view engine', 'html');

router_red.get('/', function(req, res, next) {
	res.render('red');
});
router_blue.get('/', function(req, res, next) {
	res.render('blue');
});

app.get('/test', function(req, res, next) {
	console.log(req.headers);
	res.send("test");
});
app.use('/dologin', function(req, res, next) {
	req.session.user = {name:"xxx",id:"yyy"};
	if(req.session.user){
		res.json({status:200,user:req.session.user});//正常
	}else{
		res.json({status:401});//未登录
	}
});
app.use('/logout', function(req, res, next) {
	delete req.session.user;
});
app.use('/logintest', function(req, res, next) {
	console.log(req.session);
	if(req.session.user){
		res.json({status:200,info:"登录成功",user:req.session.user});
	}else{
		res.json({status:401,info:"未登录"});//未登录
	}
});

app.use('/red',router_red);
app.use('/blue',router_blue);

var server = app.listen(2002, function () {
	var host = server.address().address;
	var port = server.address().port;
	console.log('Example app listening at http://%s:%s', host, port);
});
